package com.dst.smart.receiver;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

import com.dst.smart.app.MainActivity;
import com.dst.smart.player.TonePlayer;

public class WifiReceiver extends BroadcastReceiver {
	static int isConect = 0, oldConect = 0;

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		String wifiInfoName = "";
		if (intent.getAction().equals(WifiManager.RSSI_CHANGED_ACTION)) {

		} else if (intent.getAction().equals(
				ConnectivityManager.CONNECTIVITY_ACTION)) {
			ConnectivityManager connectivityManager = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);

			NetworkInfo activeNetworkInfo = connectivityManager
					.getActiveNetworkInfo();
			if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) {
				WifiManager wifiManager = (WifiManager) context
						.getSystemService(Context.WIFI_SERVICE);
				WifiInfo wifiInfo = wifiManager.getConnectionInfo();
				wifiInfoName = wifiInfo.getSSID();
				isConect = 2;
			} else {
				isConect = 1;
			}
		} else if (intent.getAction().equals(
				WifiManager.WIFI_STATE_CHANGED_ACTION)) {// wifi打开与否
			int wifistate = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
					WifiManager.WIFI_STATE_DISABLED);

			if (wifistate == WifiManager.WIFI_STATE_DISABLED) {
			} else if (wifistate == WifiManager.WIFI_STATE_ENABLED) {
				isConect = 1;
			}
		}
		if (isConect > 0) {
			if (isConect != oldConect) {
				oldConect = isConect;
				if (!MainActivity.DoNotDisturb) { // 勿干擾
					if (oldConect == 1) {
//						TonePlayer.playTone(MainActivity.activity,
//								DemoConstant.LOADING_NET);
					} else if (oldConect == 2) {
						if((!MainActivity.musicPlaySt)&&(!MainActivity.musicTTsSt)) {
						//	MainActivity.playSound(1, "我是叮咚,连接到网络 " + wifiInfoName);
						}
					}
				}
			}
		}
	}
}